package com.health.mapp.impl;

import java.util.Date;
import java.util.List;

import org.apache.http.util.TextUtils;

import com.health.commons.PropertiesService;
import com.health.db.model.ImMsgNotice;
import com.health.db.model.ImMsgNoticeExample;
import com.health.db.model.UserLIST;
import com.health.db.model.UserPreRegisterInfo;
import com.health.db.service.msg.ImMsgNoticeService;
import com.health.db.service.user.IUserLISTService;
import com.health.db.service.user.IUserPreRegisterInfoService;
import com.health.mapp.bean.ActivateBysmsReq;
import com.health.mapp.bean.ActivateBysmsResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

public class ActivateBysmsImpl implements ActionInterface{

    private IUserLISTService userLISTService;
    private ImMsgNoticeService imMsgNoticeService ;
    private IUserPreRegisterInfoService userPreRegisterInfoService;


    @Override
    public Response execute(Header header, Object reqBean) throws Exception {
        ActivateBysmsReq req= (ActivateBysmsReq) reqBean;
        ActivateBysmsResp _resp=new ActivateBysmsResp();

        if (TextUtils.isEmpty(req.getUserNo()))
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("手机号不能为空");

        }else if (TextUtils.isEmpty(req.getSmsCode()))
        {
            _resp.setErrorCode(-1);
            _resp.setErrorMsg("短信验证码不能为空");

        }else
        {
            UserPreRegisterInfo preRegisterInfo=userPreRegisterInfoService.getByUserNo(req.getUserNo());
            if (null==preRegisterInfo)
            {
                _resp.setErrorCode(-1);
                _resp.setErrorMsg("注册信息为空");
            }else
            {
                ImMsgNoticeExample notice = new ImMsgNoticeExample();
                notice.setRandom(req.getSmsCode());
                notice.setNoticeReceiveAddress(req.getUserNo());
                notice.setIsUsedFlag((short) 1);
                notice.setSendReceiveFlag((short) 2);

                List<ImMsgNotice> list=imMsgNoticeService.selectByExample(notice);

                if (list.size()!=1) {
                    _resp.setErrorCode(ResultCode.SMS_ERROR);
                    _resp.setErrorMsg(ResultCode.SMS_ERROR_NOTE);
                    return _resp;
                }

                imMsgNoticeService.updateForUserd(notice);


                UserLIST userLIST =new UserLIST();
                Date now = DateUtils.now();

                userLIST.setUserName(preRegisterInfo.getUserName());
                userLIST.setApptoken("");
                userLIST.setUserNo(preRegisterInfo.getUserNo());
                userLIST.setPasswordCreateDate(now);
                userLIST.setLoginDatetime(now);
                userLIST.setLogoutDatetime(now);
                userLIST.setPassword(preRegisterInfo.getPassword());
                userLIST.setCreateDate(now);
                userLIST.setUpdateDate(now);
                userLIST.setRegisterDate(now);
                userLIST.setMachinedeviceid(header.getUuId());

                //user :1 doctor:3
                userLIST.setTypeid(1);
                userLIST.setTypeName("用户");
                userLIST.setIsUsedFlag((short) 1);
                //user/userdefault.png日
                userLIST.setIconUrl(PropertiesService.getString("iconUrl.userDefaultUrl").trim());
                userLISTService.insertUser(userLIST);
                userPreRegisterInfoService.deleteUserPreRegisterInfoByUserNo(preRegisterInfo.getUserNo());
                return _resp;
            }
        }

        return _resp;
    }

    public ImMsgNoticeService getImMsgNoticeService() {
        return imMsgNoticeService;
    }

    public void setImMsgNoticeService(ImMsgNoticeService imMsgNoticeService) {
        this.imMsgNoticeService = imMsgNoticeService;
    }

    public IUserPreRegisterInfoService getUserPreRegisterInfoService() {
        return userPreRegisterInfoService;
    }

    public void setUserPreRegisterInfoService(IUserPreRegisterInfoService userPreRegisterInfoService) {
        this.userPreRegisterInfoService = userPreRegisterInfoService;
    }

    public IUserLISTService getUserLISTService() {
        return userLISTService;
    }

    public void setUserLISTService(IUserLISTService userLISTService) {
        this.userLISTService = userLISTService;
    }
}
